import folium
import pandas as pd

#san_map.save("./test.html")

# define the world map
world_map = folium.Map()
# display world map
world_map

#2. 输入经纬度，尺度，在这里我们以旧金山（37.7749° N, 122.4194° W）为例。
# San Francisco latitude and longitude values
latitude = 37.77
longitude = -122.42
# Create map and display it
san_map = folium.Map(location=[latitude, longitude], zoom_start=12)
# Display the map of San Francisco
san_map

#更改地图显示，默认为'OpenStreetMap'风格，我们还可以选择'Stamen Terrain', 'Stamen Toner
# Create map and display it
san_map = folium.Map(location=[latitude, longitude], zoom_start=12,tiles='Stamen Toner')

#3. 读取数据集（旧金山犯罪数据集）
# Read Dataset
cdata = pd.read_csv('https://cocl.us/sanfran_crime_dataset')
cdata.head()

#4. 在地图上显示前200条犯罪数据
# get the first 200 crimes in the cdata
limit = 200
data = cdata.iloc[0:limit, :]

# Instantiate a feature group for the incidents in the dataframe
incidents = folium.map.FeatureGroup()

# Loop through the 200 crimes and add each to the incidents feature group
for lat, lng, in zip(cdata.Y, data.X):
    incidents.add_child(
        folium.CircleMarker(
            [lat, lng],
            radius=7, # define how big you want the circle markers to be
            color='yellow',
            fill=True,
            fill_color='red',
            fill_opacity=0.4
        )
    )

# Add incidents to map
san_map = folium.Map(location=[latitude, longitude], zoom_start=12)
san_map.add_child(incidents)

#5. 添加地理标签
# add pop-up text to each marker on the map
latitudes = list(data.Y)
longitudes = list(data.X)
labels = list(data.Category)

for lat, lng, label in zip(latitudes, longitudes, labels):
    folium.Marker([lat, lng], popup=label).add_to(san_map)

# add incidents to map
san_map.add_child(incidents)

san_map.save("./test.html")


